package com.lvwenguo.service.serviceImple;

import com.lvwenguo.dao.SaleDao;
import com.lvwenguo.model.SaleData;
import com.lvwenguo.service.SaleInter;
import com.lvwenguo.utils.JdbcUtil;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SaleImple implements SaleInter {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Connection connection;
    private JdbcUtil jdbcUtil;
    private SaleDao saleDao;
    public SaleImple(){
        connection=JdbcUtil.getJdbcUtil().getConnertion();
        saleDao=new SaleDao(connection);

    }
    //用于添加模拟销售
    @Override
    public String AddSale(SaleData saleData, String name) {
        /* 添加模拟销售表信息，首先查询出库存数量，根据库存数量去判断是否可以销售
         * 可以销售，查询出进货单价，添加模拟销售表信息，更新库存，更新库存信息表。
         * */
        //查询库存
        try {
            connection.setAutoCommit(false);
            String selectCount="select s.stock_count from biz_stock s where s.product_bar_code=?";
            Object[] objects={saleData.getProduct_bar_code()};
            SaleData saleData1 = saleDao.selectOnt(selectCount,objects,SaleData.class);
            //判断大小
            if(saleData.getSale_count()>saleData1.getStock_count()){
                return "库存数量不足";
            }else{

                //查询商品id
                String selectid="select p.id from biz_product p where p.bar_code=?";
                Object[] objects1={saleData.getProduct_bar_code()};
                SaleData saleData2 = saleDao.selectOnt(selectid,objects1,SaleData.class);
                //查询进货单价
                String selectPrice="select p.purchase_price from biz_purchase p where p.product_id=?";
                Object[] objects2={saleData2.getId()};
                SaleData saleData3 = saleDao.selectOnt(selectPrice,objects2,SaleData.class);
                //即可添加模拟销售表信息
                String AddSale="INSERT into biz_sale (product_bar_code,sale_price,sale_date,sale_count,sale_amount,gross_profit,create_time,update_time,deleted)values(?,?,?,?,?,?,?,?,?)";
                Object[] objects3={saleData.getProduct_bar_code(), saleData.getSale_price(), saleData.getSale_date(), saleData.getSale_count(),( saleData.getSale_count() * saleData.getSale_price()),
                        (saleData.getSale_count() * saleData.getSale_price() - saleData3.getPurchase_price() * saleData.getSale_count()),
                        sdf.parse(sdf.format(new Date())), sdf.parse(sdf.format(new Date())), 0};
                int i = saleDao.Add(AddSale,objects3);
                if(i!=0){
                    //表示添加成功，修改库存信息
                    String updateStock="UPDATE biz_stock s set s.stock_count=? where s.product_bar_code=?";
                    Object[] objects4={ saleData1.getStock_count() - saleData.getSale_count(), saleData.getProduct_bar_code()};
                    int i1 = saleDao.update(updateStock,objects4);
                    if(i1!=0){
                        //查询用户id
                        String selectsessionnameId="select u.id from sys_user u where u.login_account=?";
                        Object[] objects5={name};
                        SaleData sa = saleDao.selectOnt(selectsessionnameId,objects5,SaleData.class);
                        //添加库存修改记录表
                        String Addmodify="INSERT into biz_stock_modify_record (product_bar_code,operator_id,modify_count,modify_type,create_time,update_time,deleted) values(?,?,?,?,?,?,?)";
                        Object[] objects6={saleData.getProduct_bar_code(), sa.getId(), saleData.getSale_count(), "销售", sdf.parse(sdf.format(new Date())), sdf.parse(sdf.format(new Date())), 0};
                        int i2 =saleDao.Add(Addmodify,objects6);
                        if(i2!=0){
                            connection.commit();
                            return "销售成功";
                        }
                        else{
                            try {
                                connection.rollback();
                            } catch (SQLException ex) {
                                ex.printStackTrace();
                            }
                            return "销售失败";
                        }
                    }
                    else{
                        try {
                            connection.rollback();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }

                        return "库存更新失败";
                    }
                }else{
                    try {
                        connection.rollback();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                    //表示添加失败
                    return "信息更新失败";
                }
            }



        } catch (SQLException | ParseException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }

        return "错误";
    }

    @Override
    public double selectPrice(String bar) {
        String sql="select p.sale_price from biz_product p where p.bar_code=?";
        Object[] objects={bar};
        SaleData saleData = null;
        try {
            saleData = saleDao.selectOnt(sql, objects, SaleData.class);

        if(saleData==null){

            return 0;
        }else{
            return saleData.getSale_price();
        }

    } catch (Exception e) {
         return 0;
    }
    }
    //销售报表
    @Override
    public List<SaleData> selectSale() {
        String sql="select s.product_bar_code,p.product_name,s.sale_price,s.sale_count,s.sale_amount,s.sale_date from biz_sale s,biz_product p where s.product_bar_code=p.bar_code and p.deleted=0;";
        try {
            return saleDao.selectList(sql,SaleData.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//模糊查询
    @Override
    public List<SaleData> selectSaleMohu(String name) {
        String sql="select s.product_bar_code,p.product_name,s.sale_price,s.sale_count,s.sale_amount,s.sale_date from biz_sale s,biz_product p where s.product_bar_code=p.bar_code and p.deleted=0 and p.product_name like ?";
        Object[] objects={"%"+name+"%"};
        try {
            return saleDao.selectList(sql,objects,SaleData.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
